package com.haocang.szgm.ui.user.activity;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.haocang.commonlib.loadingdialog.LoadingDialog;
import com.haocang.commonlib.network.interfaces.InitDataCallBack;
import com.haocang.commonlib.network.request.MD5Utils;
import com.haocang.commonlib.otherutil.ActivityFunctionUnit;
import com.haocang.commonlib.otherutil.TextUtils;
import com.haocang.szgm.R;
import com.haocang.szgm.activity.comm.CommFinalActivity;
import com.haocang.szgm.activity.tool.CacheActivityUtil;
import com.haocang.szgm.requestcenter.DataModel;
import com.haocang.szgm.ui.home.activity.HomeActivity;
import com.haocang.szgm.ui.login.activity.LoginActivity;
import com.haocang.szgm.ui.login.bean.UserInfoBean;
import com.haocang.szgm.ui.waterpoint.activity.WaterPointEditActivity;
import com.haocang.szgm.utils.config.Config;
import com.haocang.szgm.utils.config.WebApi;
import com.haocang.szgm.utils.tools.CacheData;
import com.haocang.szgm.utils.tools.CustomDialog;
import com.haocang.szgm.utils.tools.SharedPreferencesUtil;

import net.tsz.afinal.annotation.view.ViewInject;

import java.util.HashMap;

public class UpdatePassActivity extends CommFinalActivity implements ActivityFunctionUnit {


    @ViewInject(id = R.id.et_oldpass)
    EditText et_oldpass;//旧密码
    @ViewInject(id = R.id.et_nowpass1)
    EditText et_nowpass1;//新密码
    @ViewInject(id = R.id.et_nowpass2)
    EditText et_nowpass2;//确认密码
    @ViewInject(id = R.id.submit)
    Button submit;//提交按钮

    private Context context;
    private LoadingDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = UpdatePassActivity.this;
        //添加到栈
        if (!CacheActivityUtil.activityList.contains(context)) {
            CacheActivityUtil.addActivity(UpdatePassActivity.this);
        }

        initView();
        initEvent();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CacheActivityUtil.finishSingleActivity(UpdatePassActivity.this);
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_update_pass;
    }

    @Override
    protected boolean showNavigation() {
        return true;
    }

    @Override
    protected int showMoreIcon() {
        return 0;
    }

    @Override
    protected String showTitleName() {
        return "修改密码";
    }

    @Override
    public void initView() {

    }

    @Override
    public void initEvent() {


        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });


        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String ls_oldpass = et_oldpass.getText().toString();
                String ls_nowpass1 = et_nowpass1.getText().toString();
                String ls_nowpass2 = et_nowpass2.getText().toString();


                if (ls_oldpass.equals("")
                        || ls_nowpass1.equals("")
                        || ls_nowpass2.equals("")
                        ) {
                    ToastShow("密码不能为空！");
                    return;
                }

                if (!ls_nowpass1.equals(ls_nowpass2)) {
                    ToastShow("输入的新密码不一致！");
                    return;
                }
                if (ls_nowpass1.length() < 6) {
                    ToastShow("输入的新密码不能小于6位！");
                    return;
                }


                CustomDialog.Builder builder = new CustomDialog.Builder(context);
                builder.setTitle("提示");
                builder.setMessage("是否确定修改密码？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //设置你的操作事项

                        UpdatePass();

                    }
                });

                builder.setNegativeButton("取消",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();

                            }
                        });
                builder.create().show();


            }

        });
    }

    @Override
    public void initData() {

    }


    private void UpdatePass() {

        String ls_oldpass = et_oldpass.getText().toString();
        String ls_nowpass1 = et_nowpass1.getText().toString();


        HashMap<String, Object> maps = new HashMap<>();
        maps.put("UserID", CacheData.UserID());
        maps.put("PassWord", MD5Utils.MD5(ls_oldpass));
        maps.put("NowPassword", MD5Utils.MD5(ls_nowpass1));

        dialog = new LoadingDialog(context, "修改密码中");
        dialog.show();
        DataModel.PostApiRequest(mContext, Config.host + WebApi.Api_UpdatePass, maps, new InitDataCallBack() {

            @Override
            public void callbak(boolean b, String string) {
                if (b) {
                    ToastShow("修改成功,请重新登录！");
                    SharedPreferencesUtil.SaveUserInfo(context, "", "", "", "", "", "", "", "N");
                    CacheActivityUtil.finishActivity();
                } else {
                    ToastShow("修改失败," + string);
                }
                dialog.close();
            }
        });


    }


    //退出程序
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            return super.onKeyDown(keyCode, event);
        } else {
            return false;
        }
    }

}
